package com.asking.mq;

import com.asking.domain.History;
import com.asking.service.HistoryService;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.jms.MapMessage;
import javax.jms.Message;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class HistoryConsumer {

    @Resource
    private HistoryService  historyService;
    @JmsListener(destination = "HISTORY.JOIN")
    public   void   recive(Message message) throws Exception {
        MapMessage mapMessage= (MapMessage)message;
        String uid = mapMessage.getString("uid");
        String qid = mapMessage.getString("qid");
        String cid = mapMessage.getString("cid");
        String score = mapMessage.getString("score");
        String historyTime = mapMessage.getString("historyTime");
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date parse = dateFormat.parse(historyTime);
        History  history  =new History();
        history.setCid(Integer.parseInt(cid));
        history.setQid(Integer.parseInt(qid));
        history.setUid(Integer.parseInt(uid));
        history.setScore(Integer.parseInt(score));
        history.setHistoryTime(parse);
        historyService.addHistory(history);
    }
}
